package com.china08.yxyapi.api.manage.stu.mystudy.mistakes;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.es.ElasticYxyYxyQuesResource;
import com.china08.yxyapi.entity.mg.mistakes.StuMistakesQues;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.mistakes.StuMistakesQuesRepository;
import com.china08.yxyapi.service.manage.tec.teaching.lecture.ElasticQuesService;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

/**
 * 错题本--学生端--巩固练习
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController
public class YxyMistakesQuesElasticQuesSrhController extends YxyApiBaseController {

    @Autowired
    private ElasticQuesService elasticQuesService;

    @Autowired
    private StuMistakesQuesRepository stuMistakesQuesRepository;

    @RequestMapping(value = "/api_yw/manage/stu/mystudy/mistakes/elastic/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel)
            throws ParseException, JsonParseException, JsonMappingException, IOException {
        String quesId = reqModel.quesId;
        List<String> quesIdList = new ArrayList<String>();
        quesIdList.add(quesId);
        StuMistakesQues ques = stuMistakesQuesRepository.findTOP1ByQuesId(quesId);

        String questionType = "1";

        List<Integer> knowIdList = ques.getKnowIdLst();
        String difficultLevel = "3";
        List<Question> returnList = new ArrayList<>();

        List<ElasticYxyYxyQuesResource> elastic = elasticQuesService.findTop3ByQuestionTypeAndQuesIdNotIn(questionType,
                knowIdList, difficultLevel, quesIdList);
        RespModel model = new RespModel();
        if (elastic.size() < 3) {
            model.questionLst = null;
            return model;
        }

        for (ElasticYxyYxyQuesResource elasticYxyYxyQuesResource : elastic) {
            Question question = new Question();
            question.ques = elasticYxyYxyQuesResource.getQuesBody();
            question.questionType = elasticYxyYxyQuesResource.getQuestionType();
            returnList.add(question);
        }
        model.questionLst = returnList;
        return model;
    }

    /* 请求参数 */
    protected static class ReqModel extends YxyApiPageReqModel {
        // id
        public String quesId;
    }

    /* 响应参数 */
    protected static class RespModel extends YxyApiRespModel {
        public List<Question> questionLst;
    }

    protected static class Question {
        // 试题
        public Object ques;
        // 1 单选 8多选
        public String questionType;
    }

}
